package com.bhs.sansong.ui.login;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.Constants;
import com.bhs.sansong.bean.MenuBean;
import com.bhs.sansong.bean.OperationEvent;
import com.bhs.sansong.bean.Permission;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.BottomSelectListDialog;
import com.bhs.sansong.ui.dialog.MyClickListener2;
import com.bhs.sansong.ui.main.MainActivity;
import com.bhs.sansong.ui.main.SanSongFragment;
import com.bhs.sansong.utils.LocationUtils;
import com.bhs.sansong.utils.LogUtils;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 选择行业
 */
public class SelectIndustryActivity extends BaseActivity implements View.OnClickListener, AMapLocationListener , NetCallBack {


    private TextView mTvIndustry;
    private TextView mTvCity;
    private Button mBtnEnter;
    private final LocationUtils locationUtils = new LocationUtils();

    @Override
    public int initLayout() {
        return R.layout.activity_select_industry;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        mTvIndustry = findViewById(R.id.tv_industry);
        mTvCity = findViewById(R.id.tv_city);
        mBtnEnter = findViewById(R.id.btn_enter);
        mBtnEnter.setOnClickListener(this);
        mTvIndustry.setOnClickListener(this);
        mTvCity.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_industry:
                bottomSelect();
                break;
            case R.id.tv_city:
                selectCity();
                break;
            case R.id.btn_enter:
                startActivity(new Intent(mActivity, MainActivity.class));
                break;
        }
    }

    private void selectCity() {
        CityPicker.from(this).setLocatedCity(null).setOnPickListener(new OnPickListener() {
            @Override
            public void onPick(int position, City data) {
                mTvCity.setText(data.getName());
                SharedPreferencesUtils.saveString(mActivity, "city", data.getName());
                verify();
            }

            @Override
            public void onCancel() {
                LogUtils.d("取消选择");
            }

            @Override
            public void onLocate() {
                locationUtils.methodRequiresTwoPermission(mActivity, SelectIndustryActivity.this);
            }
        }).show();
    }

    private void verify() {
        boolean verify = true;
        if (TextUtils.isEmpty(mTvCity.getText()) || TextUtils.isEmpty(mTvIndustry.getText())) {
            verify = false;
        }
        mBtnEnter.setEnabled(verify);
        mBtnEnter.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
        if(verify){
            networkRequest(netApi.save_industry_city(mTvIndustry.getText().toString(),mTvCity.getText().toString()),this);
        }
    }

    private BottomSelectListDialog listDialog;

    private void bottomSelect() {
        if (listDialog == null) {
            List<MenuBean> list = new ArrayList<>();
            list.add(new MenuBean("公司/工厂"));
            list.add(new MenuBean("个人"));
            list.add(new MenuBean("商户"));
            list.add(new MenuBean("取消"));
            listDialog = new BottomSelectListDialog(mActivity, list);
            listDialog.setMyClickListener(new MyClickListener2() {
                @Override
                public void onClick(String id, String info) {
                    mTvIndustry.setText(id);
                    verify();
                }
            });
        }
        listDialog.show();
    }

    @Subscribe
    public void onEvent(Permission event) {
        locationUtils.methodRequiresTwoPermission(mActivity, this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        CityPicker.from(SelectIndustryActivity.this).locateComplete(new LocatedCity(aMapLocation.getCity(), aMapLocation.getProvince(), aMapLocation.getCityCode()), LocateState.SUCCESS);
        Constants.latitude = aMapLocation.getLatitude();
        Constants.longitude = aMapLocation.getLongitude();
        Constants.city = aMapLocation.getCity();
        Constants.province = aMapLocation.getProvince();
        Constants.cityCode = aMapLocation.getCityCode();
        mTvCity.setText(aMapLocation.getCity());
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {

    }

    @Override
    public void onFailure(int requestCode) {

    }
}